package org.water.garden.msn.activity;

import org.water.garden.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Im.Account;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

/**
 * @author sshwsfc@gmail.com
 * 
 * MSN Account Editer
 */
public class AccountEditer extends Activity {

    private static final String TAG = "MSNAccountEditer";

    private static final int ACCOUNT_INDEX = 1;
    private static final int PWD_INDEX = 2;

    private static final int COMPLETE_ID = Menu.FIRST;
    private static final int REVERT_ID = Menu.FIRST + 3;
    private static final int DISCARD_ID = Menu.FIRST + 1;
    private static final int DELETE_ID = Menu.FIRST + 2;

    private static final int PROVIDER_ID = 5;
    
    private static String[] PROJECTION = new String[] {
    		Account._ID, // 0
    		Account.NAME, // 1
    		Account.PASSWORD, // 2
    		Account.KEEP_SIGNED_IN // 3
    };
    
    private Cursor mCursor;
    private EditText accText;
    private EditText pwdText;
    private String mOriginalContent;
    
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

        final Intent intent = getIntent();
		this.setContentView(R.layout.msn_account_edit);
        
        accText = (EditText)findViewById(R.id.gtalk_account_label);
        pwdText = (EditText)findViewById(R.id.gtalk_pwd_label);
        
        if(null == intent.getAction()){
            Log.e(TAG, "Action is null ");
            finish();
            return;
        }
        
        if (intent.getAction().equals(Intent.EDIT_ACTION)) {
            setTitle(getText(R.string.account_insert));
        } else if (intent.getAction().equals(Intent.INSERT_ACTION)) {
            setTitle(getText(R.string.account_edit));
        }
	}


    protected void putData() {
        if (mCursor != null) {
            mCursor.first();
            
            String account = mCursor.getString(ACCOUNT_INDEX);
            accText.setTextKeepState(account);
            
            if (mOriginalContent == null) {
                mOriginalContent = account;
            }
        } 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, COMPLETE_ID, R.string.account_complete, R.drawable.account_add).setShortcut('0', 'c');
        if (getIntent().getAction().equals(Intent.EDIT_ACTION)) {
            menu.add(0, REVERT_ID, R.string.account_revert, R.drawable.account_check).setShortcut('1', 'r');
            menu.add(0, DELETE_ID, R.string.account_delete, R.drawable.account_remove).setShortcut('2', 'd');
        } else {
            menu.add(0, DISCARD_ID, R.string.account_discard, R.drawable.account_check).setShortcut('1', 'd');
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        // Handle all of the possible menu actions.
        switch (item.getId()) {
        case COMPLETE_ID:
        	saveAccount();        	
            finish();
            break;
        case DELETE_ID:
            deleteAccount();
            finish();
            break;
        case DISCARD_ID:
            cancelAccount();
            break;
        case REVERT_ID:
            cancelAccount();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveAccount() {
    	ContentValues cvs = new ContentValues();
    	
    	cvs.put(Account.NAME, accText.getText().toString());
    	cvs.put(Account.PASSWORD, pwdText.getText().toString());
    	cvs.put(Account.PROVIDER, PROVIDER_ID);
    	
    	Uri mURI;
    	
        if (mCursor != null) {
            mCursor.updateString(ACCOUNT_INDEX, cvs.getAsString(Account.NAME));
            mCursor.updateString(PWD_INDEX, cvs.getAsString(Account.PASSWORD));

            managedCommitUpdates(mCursor);
            mURI = getIntent().getData();
        }else{
        	mURI = getContentResolver().insert(Account.CONTENT_URI, cvs);
        }

        setResult(RESULT_OK, mURI.toString());
	}

    private final void cancelAccount() {
        setResult(RESULT_CANCELED);
        finish();
    }

    private final void deleteAccount() {
        if (mCursor != null) {
            accText.setText("");
            pwdText.setText("");
            mCursor.deleteRow();
            mCursor.deactivate();
            mCursor = null;
        }
    }
}
